﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.ModelView;

namespace HotDealD2.Models.ModelManager
{
    public class StatsModel
    {
        private QLDataBaseDataContext dataContext;
        public StatsModel()
        {
            dataContext = new QLDataBaseDataContext();
        }
        public List<StatsView> CaculatorByBusiness()
        {

            List<StatsView> statsview = new List<StatsView>();
            AccountModel acountmodel = new AccountModel();
            IList<ThanhVien> listbusiness = acountmodel.GetListAllBusinessAccount();
        
           
            List<Deal> deal = new List<Deal>();
            List<DanhSachSanPham> danhsachsanpham = new List<DanhSachSanPham>();
            for (int i = 0; i < listbusiness.Count; i++)
            {
                StatsView temp = new StatsView();
                temp.ThanhVien = listbusiness[i];
                int DoanhThu = 0;
                deal = (from order in dataContext.Deals
                                   where order.UsernameDoanhNghiep== listbusiness[i].Username
                                   select order).ToList();
                 for (int j = 0; j < deal.Count; j++)
                 {
                     danhsachsanpham = (from order in dataContext.DanhSachSanPhams
                             where order.MaDeal == deal[j].MaDeal
                             select order).ToList();
                     for (int k = 0; k < danhsachsanpham.Count; k++)
                     {
                         
                         DoanhThu += decimal.ToInt32(danhsachsanpham[k].TongTien);
                     }
                    
                 }
                 temp.Revenue = DoanhThu;
                 statsview.Add(temp);

            }
           
            return statsview;
            
        }

        public List<StatsView> CaculatorByDeal()
        {

            List<StatsView> statsview = new List<StatsView>();
            DealModel dealmodel = new DealModel();
            IList<Deal> listdeal = dealmodel.GetAllDeal();


           
            List<DanhSachSanPham> danhsachsanpham = new List<DanhSachSanPham>();
  
                for (int j = 0; j < listdeal.Count; j++)
                {
                    StatsView temp = new StatsView();
                    temp.Deal = listdeal[j];
                    int DoanhThu =0;
                    danhsachsanpham = (from order in dataContext.DanhSachSanPhams
                                       where order.MaDeal == listdeal[j].MaDeal
                                       select order).ToList();
                    for (int k = 0; k < danhsachsanpham.Count; k++)
                    {

                        DoanhThu += decimal.ToInt32(danhsachsanpham[k].TongTien);
                    }
                     temp.Revenue = DoanhThu;
                     statsview.Add(temp);

                }
               

       

            return statsview;

        }

        public List<StatsView> CaculatorByMonth()
        {
            List<StatsView> statsview = new List<StatsView>();
            DealModel dealmodel = new DealModel();
            IList<Deal> listdeal = dealmodel.GetAllDeal();
            List<DanhSachSanPham> danhsachsanpham = new List<DanhSachSanPham>();
            List<DonHang> donhang = new List<DonHang>();
            
            for (int j = 1; j <=12; j++)
            {
                StatsView temp = new StatsView();
                temp.Thang = j.ToString();
                int DoanhThu = 0;
                string thang = j.ToString("00") + "/2012";
                string ngaydauthang = j.ToString("00") + "/01" + "/2012";
                string ngaycuoithang =  j.ToString("00") + "/30" + "/2012"; 
                switch (j)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        ngaycuoithang = j.ToString("00") + "/31" + "/2012"; 
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                         ngaycuoithang = j.ToString("00") + "/30" + "/2012"; 
                        break;
                    case 2:
                        ngaycuoithang = j.ToString("00") + "/29" + "/2012"; 
                        break;
                    
                }
                
                DateTime dt = new DateTime();
                DateTime ct = new DateTime();
               
                dt = Convert.ToDateTime(ngaydauthang);
                ct = Convert.ToDateTime(ngaycuoithang);
                donhang = (from order in dataContext.DonHangs
                                   where (order.NgayDat <= ct && order.NgayDat >= dt)
                                   select order).ToList();

              
                for (int k = 0; k < donhang.Count; k++)
                {

                    DoanhThu += decimal.ToInt32(donhang[k].TongTien);
                }
                temp.Revenue = DoanhThu;
                temp.Thang = thang;
                statsview.Add(temp);

            }




            return statsview;

        }
    }
}